package com.wicket.okrapp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrapp.base.service.MOmsOfficialDocumentService;
import com.wicket.okrapp.base.service.dto.req.*;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.common.util.CommonFunctionHelper;
import com.wicket.okrapp.dal.mapper.mbg.OmsOfficialDocumentMapper;
import com.wicket.okrapp.dal.po.mbg.OmsOfficialDocument;

import java.util.*;
import java.util.ArrayList;
//import com.wicket.okrapp.base.service.dto.res.*;

import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MOmsOfficialDocumentServiceImpl
  extends ServiceImpl<OmsOfficialDocumentMapper, OmsOfficialDocument>
  implements MOmsOfficialDocumentService {

  @Autowired
  OmsOfficialDocumentMapper omsOfficialDocumentMapper;
/**
   * 3-9-01新增公文[7919]
   * gen by moon at 11/12/2023, 10:02:56 PM
   */
 @Trace(operationName = "3-9-01新增公文")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addOfficialDocument(OmsOfficialDocument omsOfficialDocument) {
     omsOfficialDocument.setOfficialDocumentId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
omsOfficialDocument.setIsArchive("FALSE");
omsOfficialDocument.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
omsOfficialDocument.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(omsOfficialDocument!=null && omsOfficialDocument.getSubjectLifeCycle()!=null &&  omsOfficialDocument.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsOfficialDocument.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
omsOfficialDocument.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
omsOfficialDocument.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(omsOfficialDocument!=null && omsOfficialDocument.getSubjectLifeCycle()!=null && omsOfficialDocument.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             omsOfficialDocument.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
omsOfficialDocument.setIsValid("FALSE");
omsOfficialDocument.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
omsOfficialDocument.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
     if(super.save(omsOfficialDocument)){
        return omsOfficialDocument.getOfficialDocumentId();
     }else {
        return "-1";
     }
    
  }
/**
   * 3-9-01修改公文[7922]
   * gen by moon at 11/12/2023, 10:02:57 PM
   */
 @Trace(operationName = "3-9-01修改公文")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateOfficialDocument(OmsOfficialDocument omsOfficialDocument) {
     omsOfficialDocument.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(omsOfficialDocument!=null && omsOfficialDocument.getSubjectLifeCycle()!=null &&  omsOfficialDocument.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsOfficialDocument.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
omsOfficialDocument.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(omsOfficialDocument!=null && omsOfficialDocument.getSubjectLifeCycle()!=null && omsOfficialDocument.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             omsOfficialDocument.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
        return super.updateById(omsOfficialDocument);
  }
/**
   * 3-9-01查询公文列表[7926]
   * gen by moon at 11/12/2023, 10:03:01 PM
   */
 @Trace(operationName = "3-9-01查询公文列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsOfficialDocument>  queryOfficialDocumentList(QueryOfficialDocumentListReq reqDto) {
     
        try {
            QueryWrapper<OmsOfficialDocument> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsOfficialDocument.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            // TODO: 2023/12/19 去掉公文名称模糊查询 
            List<FieldInfo> likeList = new ArrayList<>();
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsOfficialDocument.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<OmsOfficialDocument>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
/**
   * 3-9-01查询公文详情[7929]
   * gen by moon at 11/12/2023, 10:03:04 PM
   */
 @Trace(operationName = "3-9-01查询公文详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  OmsOfficialDocument  queryOfficialDocumentDetail(QueryOfficialDocumentDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<OmsOfficialDocument> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsOfficialDocument> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                OmsOfficialDocument retData = retList.get(0);
                retData.setOfficialDocumentFileAddress(CommonFunctionHelper.getFilePath(retData.getOfficialDocumentFileAddress()));//otherModel: ALL_PATH

                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }
/**
   * 3-9-01执行模糊查公文列表[7935]
   * gen by moon at 11/12/2023, 10:03:08 PM
   */
 @Trace(operationName = "3-9-01执行模糊查公文列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsOfficialDocument>  implementVagueQueryOfficialDocument(ImplementVagueQueryOfficialDocumentReq reqDto) {
     
        try {
            QueryWrapper<OmsOfficialDocument> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            if(reqDto.getCreateTime()!=null){
                queryWrapper.ge("create_time",reqDto.getCreateTime());
                reqDto.setCreateTime(null);
            }
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsOfficialDocument.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsOfficialDocument.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsOfficialDocument.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
queryWrapper.orderByDesc("operate_time");
            return ((List<OmsOfficialDocument>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
/**
   * 3-9-01删除公文[7940]
   * gen by moon at 11/12/2023, 10:03:08 PM
   */
 @Trace(operationName = "3-9-01删除公文")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  deleteOfficialDocument(String officialDocumentId ) {
     
      return super.removeById(officialDocumentId);
  }
/**
   * 3-9-01批量查询模糊查公文[7945]
   * gen by moon at 11/12/2023, 10:03:12 PM
   */
 @Trace(operationName = "3-9-01批量查询模糊查公文")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsOfficialDocument>  batchQueryVagueQueryOfficialDocument(BatchQueryVagueQueryOfficialDocumentReq reqDto) {
     
        try {
            QueryWrapper<OmsOfficialDocument> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if(reqDto.getOfficialDocumentList()==null || reqDto.getOfficialDocumentList().isEmpty()){
                    return new ArrayList<>();
                }
        queryWrapper.in("official_document_id", reqDto.getOfficialDocumentList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsOfficialDocument.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsOfficialDocument.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsOfficialDocument.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<OmsOfficialDocument>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
}
